import printerReq from '@/utils/printerReq';
import request from '@/utils/request';
import { AddPrintersParams,DeleteParams,LabelPrinterParams,PrinterCommonParams,SetVoiceTypeParams } from '../request/module/printer';
import useSettingsStore from '@/store/modules/settings';
import CryptoJS from 'crypto-js';
import axios from 'axios';
import { getToken } from '@/utils/auth';
import { ElMessage } from 'element-plus';
const settingStore = useSettingsStore();


const getCommonParams = ():PrinterCommonParams=>{
	const {user,userKey} = settingStore;
	const timestamp = Math.floor(Date.now() / 1000);
	const sign = CryptoJS.SHA1(user+userKey+timestamp).toString(CryptoJS.enc.Hex);
	return {
		user,
		timestamp,
		sign
	};
};
/**
 * 添加打印机
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const addPrinters = async (addPrintersParams: AddPrintersParams) => printerReq({
	url: '/api/openapi/xprinter/addPrinters',
	method: 'post',
	data: {...getCommonParams(),...addPrintersParams},
});
/**
 * 设置打印机语音类型
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const setVoiceType = async (setVoiceTypeParams:SetVoiceTypeParams ) => printerReq({
	url: '/api/openapi/xprinter/setVoiceType',
	method: 'post',
	data: {...getCommonParams(),...setVoiceTypeParams},
});

/**
 * 获取当前用户下所有打印机信息
 * @param param0 {Object}
 * @returns
 */
export const getPrinterList = async ({page,limit,query}:{page:number,limit:number,query?:string}) => {
	const myHeaders = new Headers();
	const {userToken} = settingStore;
	myHeaders.append('Authorization', userToken);
	const requestOptions = {
		method: 'GET',
		headers: myHeaders,
	};
	try {
		const response = await fetch(`/dev-admin/api/open/equipmentMgr/v3/open-device?page=${page}&limit=${limit}&query=${query}`, requestOptions as any);
		const res = await response.text();
		return JSON.parse(res) ;
	} catch (error) {
		return error;
	}
};

/**
 * 删除打印机
 * @param deleteParams
 * @returns
 */
export const deletePinter= async (deleteParams:DeleteParams)=>printerReq({
	url: '/api/openapi/xprinter/delPrinters',
	method: 'post',
	data: {...getCommonParams(),...deleteParams},
});


export const labelPrinter = async (labelPrinterParams:LabelPrinterParams) => printerReq({
	url: '/api/openapi/xprinter/printLabel',
	method: 'post',
	data: {...getCommonParams(),...labelPrinterParams},
});

export const syncPinter =  (str:{xqPrinters: object[]}) => request({
	url: '/manage/printer/addList',
	method: 'post',
	data: JSON.stringify(str)
});
/**
 * 本地获取打印机数据含有状态
 * @param query { Object }获取条件
 * @returns
 */
export const getPrinterData = (query?:any) => request({
	url: '/manage/printer/list',
	method: 'get',
	params:query
});
/**
 * 本地获取打印机数据不含状态
 * @param query { Object }获取条件
 * @returns
 */
export const getPrinterAll = (query?:any) => request({
	url: '/manage/printer/Alllist',
	method: 'get',
	params:query
});
/**
 * 门店获取打印机数据
 * @param query { Object }获取条件
 * @returns
 */
export const getPrinterAllData =  (query?:any) => request({
	url: '/manage/printer/Alllist',
	method: 'get',
	params:query
});
/**
 * 解绑打印机语音类型
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const unbindPrint = async (printerParams:any ) => request({
	url: '/manage/printer/unbindPrint',
	method: 'post',
	data: printerParams,
});
/**
 * 解绑打印机语音类型
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const bindPrint = async (printerParams:any ) => request({
	url: '/manage/printer/addPrint',
	method: 'post',
	data: printerParams,
});
/**
 * 总部绑定打印机
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const zbBindPrint = async (printerParams:{printerKey:string[],headquartersId:string} ) => request({
	url: '/manage/printer/addZbPrint',
	method: 'post',
	data: printerParams,
});
/**
 * 门店绑定打印机
 *
 * @param {object} loginParam 添加打印机参数
 * @returns
 */
export const mdBindPrint = async (printerParams:{printerKey:string[],StoreId:string} ) => request({
	url: '/manage/printer/addMdPrint',
	method: 'post',
	data: printerParams,
});

/**
 * 根据id获取打印机数据
 * @param id { string }获取条件
 * @returns
 */
export const getPrinterById =  (id:string) => request({
	url: `/manage/printer/${id}`,
	method: 'get'
});
/**
 *  修改打印机管理
 *
 * @param {object} data
 */
export const updatePrinter = (data:any) => {
	return request({
		url: '/manage/printer/edit',
		method: 'put',
		data: data
	});
};

